package sunstar.com.xq.adapter

import android.content.Context
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.TextView
import com.squareup.picasso.Picasso
import fm.jiecao.jcvideoplayer_lib.JCUserActionStandard
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
import kotlinx.android.synthetic.main.card_sing_img.*
import kotlinx.android.synthetic.main.umeng_socialize_share.view.*
import sunstar.com.xq.R
import sunstar.com.xq.databean.CardData
import sunstar.com.xq.util.CircleTransform

/**
 * Created by Sun.Star on 2017/6/9.
 */
class SquareListAdapter(var data : MutableList<CardData> , var context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    val singleImgType = 0x0000001
    val imgsType = 0x0000002
    val video = 0x0000003
    val music = 0x0000004
    public var itemClickListener:AdapterView.OnItemClickListener?=null
    public var itemLongClickListener:AdapterView.OnItemLongClickListener?=null
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        holder!!.itemView.setOnClickListener {
            if(itemClickListener!=null){
                itemClickListener!!.onItemClick(null,holder.itemView,position,holder.itemId)
            }
        }
        holder!!.itemView.setOnLongClickListener{
            if(itemLongClickListener!=null){
                itemLongClickListener!!.onItemLongClick(null,holder.itemView,position,holder.itemId)
            }
            false
        }
        if (getItemViewType(position) == singleImgType){
            (holder as SquareSingleHolder).cardTitle!!.setText(data[position].cardTtitle)
            Picasso.with(context).load(data[position].cardImg).into((holder as SquareSingleHolder).cardImg)
            (holder as SquareSingleHolder).time!!.text = data[position].createdAt
            (holder as SquareSingleHolder).cardInfo!!.text = data[position].cardInfo
            (holder as SquareSingleHolder).userName!!.text = data[position].cardUserName
            Picasso.with(context).load(data[position].cardUserImg).transform(CircleTransform()).into((holder as SquareSingleHolder).userImg)
        }else if(getItemViewType(position) == video){
            (holder as SquareVideoHolder).userName!!.text = data[position].cardUserName
            (holder as SquareVideoHolder).time!!.text = data[position].createdAt
            (holder as SquareVideoHolder).player!!.setUp(data[position].cardVideo, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL,"")
            Picasso.with(context).load(data[position].cardImg).into((holder as SquareVideoHolder).player!!.thumbImageView)
            (holder as SquareVideoHolder).title!!.setText(data[position].cardTtitle)
            Picasso.with(context).load(data[position].cardUserImg).transform(CircleTransform()).into((holder as SquareVideoHolder).userImg)
        }else if(getItemViewType(position) == imgsType){
            (holder as SquareMultiHolder).userName!!.text = data[position].cardUserName
            (holder as SquareMultiHolder).time!!.text = data[position].createdAt
            Picasso.with(context).load(data[position].cardUserImg).transform(CircleTransform()).into((holder as SquareMultiHolder).userImg)
            var finalString = data[position].cardImg.split(",")
            var stringList = mutableListOf<String>()
            for (i in 0 .. finalString.size-1){
                stringList.add(finalString[i])
            }
            (holder as SquareMultiHolder).recycleData!!.adapter = ImageAdapter(context,stringList)
            (holder as SquareMultiHolder).recycleData!!.layoutManager = GridLayoutManager(context,3)
        }
    }

    override fun getItemCount(): Int = data.size

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        if(viewType == singleImgType){
            return SquareSingleHolder(LayoutInflater.from(context).inflate(R.layout.card_sing_img,parent,false))
        }else if(viewType == video){
            return SquareVideoHolder(LayoutInflater.from(context).inflate(R.layout.video_item,parent,false))
        }else if(viewType == imgsType){
            return SquareMultiHolder(LayoutInflater.from(context).inflate(R.layout.multi_img_item,parent,false))
        }
        return SquareSingleHolder(View(context))
    }

    override fun getItemViewType(position: Int): Int {
        if(!data[position].cardVideo.equals("")){
            return video
        }
        if(!data[position].cardMusic.equals("")){
            return music
        }
        if(!data[position].cardImg.equals("")){
           var finalString = data[position].cardImg.split(",")
            if(finalString.size == 1){
                return singleImgType
            }else{
                return imgsType
            }
        }
        return super.getItemViewType(position)
    }

    class SquareSingleHolder (itemView : View): RecyclerView.ViewHolder(itemView){
         var cardTitle:TextView? = null
         var cardInfo:TextView? = null
         var userName:TextView? = null
         var userImg:ImageView? = null
         var cardImg:ImageView? = null
         var time:TextView? = null
        init {
            cardTitle = itemView.findViewById(R.id.card_title) as TextView
            userName = itemView.findViewById(R.id.card_username) as TextView
            time = itemView.findViewById(R.id.card_time) as TextView
            cardInfo = itemView.findViewById(R.id.card_info) as TextView
            userImg = itemView.findViewById(R.id.card_userimg) as ImageView
            cardImg = itemView.findViewById(R.id.card_img) as ImageView
        }
    }
    class SquareMultiHolder (itemView : View): RecyclerView.ViewHolder(itemView){
        var userName:TextView? = null
        var userImg:ImageView? = null
        var recycleData:RecyclerView? = null
        var time:TextView? = null
        init {
            userName = itemView.findViewById(R.id.multi_img_username) as TextView
            time = itemView.findViewById(R.id.multi_img_time) as TextView
            userImg = itemView.findViewById(R.id.multi_img_userimg) as ImageView
            recycleData = itemView.findViewById(R.id.multi_img_rec_view) as RecyclerView
        }
    }
    class SquareVideoHolder (itemView : View): RecyclerView.ViewHolder(itemView){
        var userName:TextView? = null
        var userImg:ImageView? = null
        var time:TextView? = null
        var title:TextView? = null
        var player:JCVideoPlayerStandard? = null
        init {
            userName = itemView.findViewById(R.id.video_username) as TextView
            title = itemView.findViewById(R.id.video_title) as TextView
            time = itemView.findViewById(R.id.video_time) as TextView
            userImg = itemView.findViewById(R.id.video_userimg) as ImageView
            player = itemView.findViewById(R.id.video_player) as JCVideoPlayerStandard
        }
    }
}